﻿$(window).load(function() {
    
    $("#frVoucherDetail").submit(function () {
        var submitdata = $(this).serialize();
        $("#frVoucherDetail").hide();
        $("#VoucherDetail-MessageResult").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            method:"POST",
            url: "/voucher/SaveVoucher",
            data: submitdata,
            success: function (data) {
                $("#VoucherDetail-MessageResult").html(data);
                $("#frVoucherDetail").show();
            },
                error: function () {
                    $("#VoucherDetail-MessageResult").html("Load failed");
                    $("#frVoucherDetail").show();
                }
            
        });
        return false;
    });
   
    $("#frAddVoucher").submit(function () {
        var submitdata = $(this).serialize();
        $("#body-frVoucherDetail").hide();
        $("#Msg-Result").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            method: "POST",
            url: "/voucher/addVoucher",
            data: submitdata,
            success: function (data) {
                $("#Msg-Result").html(data);
                $("#body-frVoucherDetail").show();
            },
            error: function () {
                $("#Msg-Result").html("Load failed");
                $("#body-frVoucherDetail").show();
            }
        });
        window.opener.loadvouchers(window.opener.$("#VoucherReasonID").val());
        return false;
    });
    $("#frAddVoucherReason").submit(function () {
        var submitdata = $(this).serialize();
        $("#frAddVoucherReason").hide();
        $("#Msg-VoucherReasonResult").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            method: "POST",
            url: "/voucher/addVoucherReason",
            data: submitdata,
            success: function (data) {
                $("#Msg-VoucherReasonResult").html(data);
                $("#frAddVoucherReason").show();
            },
            error: function () {
                $("#Msg-VoucherReasonResult").html("Load failed");
                $("#frAddVoucherReason").show();
            }
        });
        return false;
    });
    $("#SearchVoucherCustomers").submit(function() {
        var submitdata = $(this).serialize();
        $("#SearchCustomerResult").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            method: "POST",
            url: "/voucher/SearchCustomer",
            data: submitdata,
            success: function (data) {
                $("#SearchCustomerResult").html(data);
            },
            error: function () {
                $("#SearchCustomerResult").html("Load failed");
            }
        });
        return false;
    });
    $("#frSearchCustomerResult").submit(function() {
        var submitdata = $(this).serialize();
        $("#dttbResult").hide();
         $("#addcustomer_messageresult").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            method: "POST",
            url: "/voucher/addcustomerstovoucher",
            data: submitdata,
            success: function (data) {
                $("#dttbResult").show();
                $("#addcustomer_messageresult").html(data);
            },
            error: function () {
                $("#addcustomer_messageresult").html("");
                $("#dttbResult").show();
            }
        });
        
        return false;
    });
});
function loadvoucherdetail(voucherid) {
    $("#body-frVoucherDetail").hide();
    $("#VoucherDetail-MessageResult").html("<div class='ajax-loadForm'></div>");
    $.ajax({
        url: "/voucher/VoucherDetail",
        data: { voucherid: voucherid },
        success: function(data) {
            $("#frVoucherDetail").html(data);
            $("#VoucherDetail-MessageResult").html("");
            $('#datetimepicker2,#datetimepicker3').datetimepicker({
                language: 'en',
                format:'MM/dd/yyyy',
                pick12HourFormat: true
            });
        },
        error: function () {
            $("#VoucherDetail-MessageResult").html("");
            $("#body-frVoucherDetail").show();
        }
    });
    loadvouchercustomers(voucherid);
}

function loadvouchercustomers(voucherid) {
    $("#frViewCustomer").hide();
    $("#VoucherCustomer-MessageResult").html("<div class='ajax-loadForm'></div>");
    $.ajax({
        url: "/voucher/viewcustomer",
        data: { voucherid: voucherid },
        success: function (data) {
            $("#CustomerList").html(data);
            $("#VoucherCustomer-MessageResult").html('');
        },
        error: function () {
            $("#VoucherCustomer-MessageResult").html('');
            $("#frViewCustomer").show();
        }
        
    });
}
function loadvouchers(reasonId) {
    $("#grVoucherCode-MessageResult").html("<div class='ajax-loadForm'></div>");
    $("#lbxVoucherCode").hide();
        $.ajax({
            url: "/voucher/GetVoucherCodes",
            data: { reasonId: reasonId },
            success: function (data) {
                $("#lbxVoucherCode").html('');
                $.each(data, function(id, option) {
                    $("#lbxVoucherCode").append($('<option></option>').val(option.id).html(option.name));
                });
                $("#grVoucherCode-MessageResult").html('');
                $("#lbxVoucherCode").show();
            },
            error: function () {
                $("#grVoucherCode-MessageResult").html('');
                $("#lbxVoucherCode").show();
            }
        });
}
function deletevoucher(voucherid) {
    if (confirm('Are you sure?')) {
        $("#body-frVoucherDetail").hide();
        $("#VoucherDetail-MessageResult").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            url: "/voucher/deletevoucher",
            data: { voucherid: voucherid },
            success: function (data) {
                $("#VoucherDetail-MessageResult").html(data);
                loadvouchers($("#VoucherReasonID").val());
            },
            error: function () {
                $("#VoucherDetail-MessageResult").html("");
                $("#body-frVoucherDetail").show();
            }
        });
    }
}

function DeleteVoucherCustomer(customerVoucherId) {
    $("#frViewCustomer").hide();
    $("#VoucherCustomer-MessageResult").html("<div class='ajax-loadForm'></div>");
    $.ajax({
        url: "/voucher/DeleteCustomer",
        data: { customerVoucherID: customerVoucherId },
        success: function (data) {
            $("#CustomerList").html(data);
            $("#VoucherCustomer-MessageResult").html('');
        },
        error: function () {
            $("#frViewCustomer").show();
            $("#VoucherCustomer-MessageResult").html('');
        }
    });
}
function loadvoucherdefault(reasonid) {
    window.location = '?reasonid=' + reasonid;
}

function addproducttolist() {
    var productId = $("#txbProductId").val();
    $("#Msg-lstProductResult").append("<div class='ajax-loadForm'></div>");
    $.ajax({
        
            url: "/voucher/GetProduct",
            data: { productId: productId },
            success: function (data) {
                if (data.id != undefined) {
                    $("#Msg-lstProductResult").html('');
                    if ($("#product" + data.id + "").val() == undefined) {
                    $("#ListProducts").append("<div class='row-fluid'>" +
                        "<div class='span2'>" +
                        "<input  id='product" + data.id + "' type='checkbox' style='vertical-align: top;'name='SelectedProducts' checked='checked' value='" + data.id + "'/>" +
                        "</div>" +
                        "<div class='span10'>" +
                        "<label for='product" + data.id + "'>" + data.name +
                        "</label>" +
                        "</div>" +
                        "</div>");
                }
                }
                else{
                    $("#Msg-lstProductResult").html(data);
                } 
            },
            error: function () {
                $("#Msg-lstProductResult").html("Load failed");
            }
    });
}

    function addCustomersToVoucher() {
        $("#frSearchCustomerResult").submit();
    };

    function searchCustomerCNO() {
        var totalRecordDisplay = $("#TotalRecordDisplay").val();
        $("#SearchCustomerResult").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            method: "POST",
            url: "/voucher/SearchCustomerCNO",
            data: {totalRecordDisplay:totalRecordDisplay},
                success: function (data) {
                    $("#SearchCustomerResult").html(data);
                }
               , error: function () {
                   $("#SearchCustomerResult").html("Load failed");
               }
            });
    }
    function ChangeGrouponOrderValue(grouponVoucherType) {
        var value = 0;
        if (grouponVoucherType == 1) {
            value = 3.8;
        }
        if (grouponVoucherType == 2) {
            value = 5.45;
        }
        if (grouponVoucherType == 3) {
            value = 7.07;
        }
        window.location=("/voucher/ManageGrouponVouchers?value=" + value);
    }
    


